package com.si.anddos;

import com.si.anddos.dpadapters.AnddosDbAdapter;
import com.si.anddos.dpadapters.DatabaseProvider;
import com.si.anddos.library.main.DownloadProviderThread;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.net.wifi.WifiManager;
import android.net.wifi.WifiManager.WifiLock;
import android.os.IBinder;
import android.widget.Toast;

/**
 * 
 * @author Carlos Martinez
 * 
 */
public class DownloadProviderService extends Service {

	private DownloadProviderThread pt;
	private DatabaseProvider dbProvider;
	public static boolean running = false;
	private NotificationManager nm;
	private WifiLock wifiLock;

	@Override
	public IBinder onBind(Intent intent) {
		
		return null;
	}

	@Override
	public void onCreate() {
		super.onCreate();
		this.nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
		
		this.pt = new DownloadProviderThread(this);
		this.pt.start();

		DownloadProviderService.running = true;
		this.displayInformation(getText(R.string.downloadServiceStarted)
				.toString());

		WifiManager wm = (WifiManager)getSystemService(WIFI_SERVICE);
		this.wifiLock = wm.createWifiLock("DownloadWifiLock");
		this.wifiLock.acquire();
	}

	public void displayInformation(String text) {
		Toast.makeText(this.getApplicationContext(), text, Toast.LENGTH_SHORT)
				.show();
	}

	//@Deprecated
	/*public void showNotification(int resId) {
		// In this sample, we'll use the same text for the ticker and the
		// expanded notification
		CharSequence text = getText(resId);

		// Set the icon, scrolling text and timestamp
		Notification notification = new Notification(R.drawable.icon, text,
				System.currentTimeMillis());

		// The PendingIntent to launch our activity if the user selects this
		// notification
		
		PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
				new Intent(this, DownloadListActivity.class), 0);

		// Set the info for the views that show in the notification panel.
		notification.setLatestEventInfo(this.getApplicationContext(),
				getText(R.string.downloadServiceLabel), text, contentIntent);

		// Send the notification.
		// We use a layout id because it is a unique number. We use it later to
		// cancel.
		nm.notify(resId, notification);

	}*/
	
	public void showNotification(long id, int resId, String file) {
		
		// In this sample, we'll use the same text for the ticker and the
		// expanded notification
		System.out.println(id);
		CharSequence text = getText(resId);

		// Set the icon, scrolling text and timestamp
		Notification notification = new Notification(R.drawable.icon, text.toString() + " " + file,
				System.currentTimeMillis());

		// The PendingIntent to launch our activity if the user selects this
		// notification
		Intent detailIntent = new Intent(this.getApplicationContext(), DownloadDetailActivity.class);
		detailIntent.putExtra(AnddosDbAdapter.colDownloadID, id);
		PendingIntent contentIntent = PendingIntent.getActivity(this.getApplicationContext(), 0,
				detailIntent, 0);

		// Set the info for the views that show in the notification panel.
		notification.setLatestEventInfo(this.getBaseContext(),
				getText(R.string.downloadServiceLabel), text, contentIntent);

		// Send the notification.
		// We use a layout id because it is a unique number. We use it later to
		// cancel.
		nm.notify((int)id, notification);
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		DownloadProviderService.running = false;
		DownloadProviderThread.setStarted(false);
		
		this.wifiLock.release();
		this.displayInformation("Service stopped");
	}
}
